package clean_handler

import "github.com/marmotedu/errors"

const (
	//  before handler
	BackupForLinkHandler HandlerBuilder = iota
	SkipSameFileHandler
	SkipSameInodeFilesHandler

	// main clean handler
	DeleteFileHandler
	LinkFileHandler
	DryRunHandler
	SuccessfulRecordHandler

	// finish handler
	ReleaseBackupHandler

	// troubleshooting handler
	RollbackHandler
	FailedRecordHandler
)

type HandlerBuilder int

var NeedInputCleanTaskInfoCollector = errors.New("need input clean task info collector")

func (b HandlerBuilder) Build() (CleanHandler, error) {
	switch b {
	case DeleteFileHandler:
		return DeleteFileHandlerImpl{}, nil
	case LinkFileHandler:
		return LinkFileHandlerImpl{}, nil
	case DryRunHandler:
		return DryRunHandlerImpl{}, nil
	case ReleaseBackupHandler:
		return ReleaseBackupHandlerImpl{}, nil
	case RollbackHandler:
		return RollbackHandlerImpl{}, nil
	case BackupForLinkHandler, SkipSameFileHandler, SkipSameInodeFilesHandler, SuccessfulRecordHandler, FailedRecordHandler:
		return nil, NeedInputCleanTaskInfoCollector
	default:
		return nil, errors.New("unknown clean handler builder")
	}
}

func (b HandlerBuilder) BuildWithCollector(collector *CleanTaskInfoCollector) (CleanHandler, error) {
	switch b {
	case BackupForLinkHandler:
		return NewBackupForLinkHandler(collector), nil
	case SkipSameFileHandler:
		return NewSkipSameFileHandler(collector), nil
	case SkipSameInodeFilesHandler:
		return NewSkipSameInodeFilesHandler(collector), nil
	case SuccessfulRecordHandler:
		return NewSucRecordHandler(collector), nil
	case FailedRecordHandler:
		return NewFailedRecordHandler(collector), nil
	default:
		return nil, errors.New("unknown clean handler builder, or builder not need collector")
	}
}
